<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Model\Art;

use App\Http\Requests;

use App\Http\Controllers\Controller;

use DB;

use Markdown;
use League\Flysystem\Directory;
use Auth;

use Captcha;
use Symfony\Component\VarDumper\Tests\Caster\reflectionParameterFixture;
use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
class NewController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    	/**
		*取数据
    	 */
//     	$res = Art::all();
    	$res = Art::orderBy('id','desc')->paginate(5);
//     	dd($res);
// /    	$res = DB::table('art')->paginate(2);
        return  view('new.index',['res'=>$res]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    	/**
    	 * 显示添加页面
    	 * 
    	 */
//     	return response()->json(array());
        return view('new.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
    	/**
		*添加数据操作
		*
    	 */
     /*    $title = $request->title;
        $content = $request->content;
      
        $res = DB::table('art')->insert(['title'=>$title,'content'=>$content]);*/
    	/**
    	 * 添加数据model方法
    	 * 
    	 */
    /* 	$art = new Art();
    	$art->title = $request->title;
    	$art->content = $request -> content;
    	$res = $art ->save(); */
    	/**
    	 * 添加数据model方法
    	 *创建数据集合
    	 */
//     	$res = Art::create(['title'=>$request->title ,'content'=>$request->content]);
		//获取并将值存入数据库
	 	/* $res = Art::create($request->except('_token'));
     	if($res){
        	return redirect('/new');
        }else{
        	return back();
        }    */
    	
    	/**
    	 * 门面validate
    	 */
    	//validate限制门面验证方式
    			/*$val = Validator::make($request->all(),[
    				'title'            => 'required|max:20|min:4',
    				'content'      => 'required|max:1500|min:10',
    				]);
    		
    		//未通过验证
    		if($val->fails()){
    			return back()->withErrors($val)->withInput();
    		} */
    	
    	/**
    	 * trait 自带验证
    	 */
   		 /* $this->validate($request, [
		    	'title'            => 'required|max:20|min:4',
		    	'content'      => 'required|max:1500|min:10',
    		],[
    			'required'  => ':attribute 是必须要的',
    		]); 
  	   */
    	
    	
	 	 //ajax传值并存入数据库
	 	 $res = Art::create($request->except('_token'));
	 	 if($res){
	 	 	$res = Art::orderBy('id','desc')->paginate(5);
// 	 	 	return  response()->json($res);
			return view('new.show',['res'=>$res]);
	 	 }  	
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    	/**
    	 * 查询数据不输出
    	 * 
    	 */
//           $res = DB::table('art') ->where('id',$id)->first();
			$res = Art::find($id);
			$res -> content = Markdown::convertToHtml($res -> content);
       	  return view('new.read',['res'=>$res]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
    	
//         $res = DB::table('art')->where('id',$id)->first();
		    $res = Art::find($id);
		    $res -> content = Markdown::convertToHtml($res -> content);
        return view('new.edit',['res'=>$res]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
    	/**
    	 * 更改
    	 * 
    	 */
//         $res = DB::table('art')->where('id',$id)->update(['title'=>$request->title,'content'=>$request->content]);
		//第一种
    	/* 	$res = Art::find($id);
    	$res -> title = $request->title;
    	$res -> content = $request -> content;
    	$res ->save(); */
    	//第二种
    	$res = Art::where('id','=',$id)->update($request->except('_token','_method'));
        return redirect('new');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
//         $res = DB::table('art')->where('id',$id)->delete();
			$res = Art::find($id)->delete();
			
 		return redirect('new');
    }
    
    //删除单条信息
    public function del($id)
    {
    	$res = Art::find($id)->delete();
    	$res = Art::orderBy('id','desc')->paginate(5);
    	return view('new.show',['res'=>$res]);
		
    }

    
    //后台登出
    public function getLogout()
    {
    	Auth::Logout();
    	return redirect('/auth/login');
    }
    
   //后台登入
   public function postLogin(Request $request)
   {
   		//判断验证码
   		$code = Captcha::check($request->verify);
		if($code){
			//判断信息是否正确
			$res = Auth::attempt(['email'=>$request->email,'password'=>$request->password]);
			if($res){
				return redirect('/new');
			}else{
				return back();
			}
		}else{
			return back();
		}
   } 
	
   //验证码
   public function getCode()
   {
   		return Captcha::img();
   }
}
